<?php

namespace app\index\model;

use app\model\Model;

class Search extends Model
{
  public $keyword;
  public $db;
  function __construct()
  {
    $this->db = $GLOBALS['db'];
    if (!isset($_GET['page'])) {
      $this->check_csrf();
    }
    if (!empty($_POST['keyword'])) {
      $_SESSION['keyword'] = str_safe($_POST['keyword']);
    }
    $this->keyword = $_SESSION['keyword'];
  }
  function index()
  {
    if (empty($this->keyword)) {
      alert_back($GLOBALS['lang']['keyword']);
    }
    // 分页&列表
    include LIB_PATH . 'cls.page.php';
    $pager = new \Page(20);
    $pager->handle($GLOBALS['db']->getOne("SELECT COUNT(id) FROM detail WHERE INSTR(d_name, '" . $this->keyword . "') ORDER BY d_order ASC, id DESC"));
    $list_pager = $GLOBALS['db']->getAll("SELECT id,d_name,d_order FROM detail WHERE INSTR(d_name, '" . $this->keyword . "') ORDER BY d_order ASC,id DESC LIMIT " . $pager->page_start . "," . $pager->page_size);
    $this->tpl('search', [
      'db' => $this->db,
      'keyword' => $this->keyword,
      'pager' => $pager,
      'list_pager' => $list_pager,
      'page' => max(intval($_GET['page']), 1)
    ]);
  }
  function __destruct()
  {
  }
}
